//
//  PKCommunityWebViewController.m
//  PianKe
//
//  Created by lanou on 16/1/3.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "PKCommunityWebViewController.h"
#import "PKCommunityWebModel.h"
#import "PKCommunityCommentTableViewCell.h"
#import "UMSocial.h"
@interface PKCommunityWebViewController () <UIWebViewDelegate,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIScrollViewDelegate>

// webView
@property (strong, nonatomic) UIWebView *webView;

// post者的具体信息postsinfo
@property (strong, nonatomic) NSMutableDictionary *postsinfoDic;
// 总的scrollView
@property (strong, nonatomic) UIScrollView *mainScrollView;
@property (strong, nonatomic) UIView *topView;
// webView的高度
@property (assign, nonatomic) CGFloat height;

// 展示评论的tableView
@property (strong, nonatomic) UITableView *commitListTableView;
// 评论的数据源
@property (strong, nonatomic) NSMutableArray *listArray;

@property (strong, nonatomic) UITextField *test;



// 评论展示的样式type
@property (strong, nonatomic) NSString *type;

// 判断是顺序还是倒叙
@property (assign, nonatomic) BOOL isAsc;


@end

@implementation PKCommunityWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _postsinfoDic = [NSMutableDictionary dictionary];
    _listArray = [NSMutableArray array];
 
#pragma mark  --- 添加导航栏 和 webview
    [self addViews];
    
       // 解析数据
    [self setUpWebData];
    
    _test = [[UITextField alloc]initWithFrame:CGRectMake(0,kScreenHeight - 50, kScreenWidth, 50)];
    _test.backgroundColor = [UIColor grayColor];
    _test.hidden = YES;
    [self.view addSubview:_test];
    _test.delegate = self;

    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    
    
}

// 添加导航栏和webView
- (void)addViews
{
    // 创建自定义导航栏
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headView];
    
    UIButton *leftBack = [UIButton buttonWithType:(UIButtonTypeCustom)];
    leftBack.frame = CGRectMake(10 * kWidthBase, 30 * kHeightBase, 30 * kWidthBase, 30 * kHeightBase);
    [leftBack setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:(UIControlStateNormal)];
    [leftBack addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [headView addSubview:leftBack];
    
    
    UIButton *commentButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    commentButton.frame = CGRectMake(kScreenWidth - 70*kWidthBase,leftBack.top,60*kWidthBase,40*kHeightBase);
    [commentButton setTitle:@"发表评论" forState:(UIControlStateNormal)];
    [commentButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    commentButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [headView addSubview:commentButton];
    [commentButton addTarget:self action:@selector(addComment:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200 * kHeightBase)];
   // self.topView.backgroundColor = [UIColor greenColor];
    self.mainScrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight * 3);
    [self.view addSubview:self.mainScrollView];
    _mainScrollView.delegate = self;
    
    [self.mainScrollView addSubview:self.topView];

    
    // 创建一个webView
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,200 * kHeightBase, kScreenWidth, self.mainScrollView.height - 200 * kHeightBase)];
    // 设置代理
    _webView.delegate = self;
    _webView.backgroundColor = [UIColor redColor];
     _webView.scrollView.bounces = NO;
    _webView.scrollView.scrollEnabled = NO;
    [self.mainScrollView addSubview:_webView];
    
    
}

#pragma mark --- 点击评论按钮 : 先判断是否为登陆,然后弹出键盘
- (void)addComment:(UIButton *)button
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    BOOL state = [user boolForKey:@"状态"];
    
    if (state == NO) {
        
        UIAlertController *failAlert = [UIAlertController alertControllerWithTitle:@"Sorry" message:@"请先登陆" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self presentViewController:failAlert animated:YES completion:nil];
        
        // 2秒后消失
        [self performSelector:@selector(failDismiss:) withObject:failAlert afterDelay:2];
        
        
    } else {
       
        [_test becomeFirstResponder];//弹出键盘
    }
    
    
    
}

// 有时候键盘不出现,可能是因为当模拟器不允许键盘使用,按comm+shift+k
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    _test.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        
        _test.frame = CGRectMake(0, kScreenHeight - height - 50, kScreenWidth, 50);
        
    }];
}
//当键盘退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    [UIView animateWithDuration:0.5 animations:^{
        
        _test.frame = CGRectMake(0, kScreenHeight - 50, kScreenWidth, 50);
        
    }];
    _test.hidden = YES;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_test resignFirstResponder];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_test resignFirstResponder];

}

#pragma mark  ---- 点击return按钮后 发布评论
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *auth = [user objectForKey:@"auth"];
        NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
        parDic[@"auth"] = auth;
        parDic[@"client"] = @"1";
        parDic[@"content"] = _test.text;
        parDic[@"contentid"] = _contentid;
        parDic[@"version"] = @"3.0.6";
        parDic[@"deviceid"] = @"F7EFB566-54F1-4912-ADFC-7E788F6D4D00";
        parDic[@"recid"] = @"";
        parDic[@"reuid"] = @"";
        
        [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/comment/add" parDic:parDic Method:POST finish:^(NSData *data) {
            
            NSLog(@"%@",data);
            
            // 这个dic里保存的是:判断你是否发布成功的话
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSString *result = dic[@"data"][@"msg"];
            
            [_test resignFirstResponder];
            
            // 操作太频繁后:
            if([result isEqualToString:@"抱歉，操作的太频繁！"])
            {
                NSLog(@"操作太频繁,请稍后在评论");
                
                // 做一个动画效果,从上端
                UIAlertController *messageAlert = [UIAlertController alertControllerWithTitle:@"Sorry" message:@"操作太频繁,请稍后在评论!" preferredStyle:(UIAlertControllerStyleAlert)];
                [self presentViewController:messageAlert animated:YES completion:nil];
                [self performSelector:@selector(failDismiss:) withObject:messageAlert afterDelay:2];
                
                
            }
            else if([result isEqualToString:@"发布成功！"]){
                // 说明发布成功!
                // 做一个动画效果,从上端
                UIAlertController *messageAlert = [UIAlertController alertControllerWithTitle:@"Congratulations" message:@"评论成功!" preferredStyle:(UIAlertControllerStyleAlert)];
                [self presentViewController:messageAlert animated:YES completion:^{
                
                    
                    [_listArray removeAllObjects];
                    
                    [self setUpWebData];
                    
                    // 因为重新加载webview后, 会重新加载topview,所以必须先移除之前topview上的子视图
                    for(UIView *view in [_topView subviews])
                    {
                        [view removeFromSuperview];
                    }
                    
                }];
                [self performSelector:@selector(failDismiss:) withObject:messageAlert afterDelay:0.5];
            
            } else {
                
                UIAlertController *messageAlert3 = [UIAlertController alertControllerWithTitle:@"Sorry" message:@"网络状况不太好" preferredStyle:(UIAlertControllerStyleAlert)];
                [self presentViewController:messageAlert3 animated:YES completion:nil];
                [self performSelector:@selector(failDismiss:) withObject:messageAlert3 afterDelay:2];
            }
            
        } error:^(NSError *error) {
            
            UIAlertController *messageAlert2 = [UIAlertController alertControllerWithTitle:@"Sorry" message:@"评论失败!" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:messageAlert2 animated:YES completion:nil];
            [self performSelector:@selector(failDismiss:) withObject:messageAlert2 afterDelay:1];
            
        }];
    
    return YES;
}


// 解析数据
- (void)setUpWebData
{

    [_listArray removeAllObjects];
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"";
    parDic[@"client"] = @"1";
    parDic[@"contentid"] = _contentid;
    parDic[@"deviceid"] = @"F7EFB566-54F1-4912-ADFC-7E788F6D4D00";
    parDic[@"version"] = @"3.0.6";
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/group/posts_info" parDic:parDic Method:POST finish:^(NSData *data) {
        
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        
        
#pragma mark ---- 评论的数据源
        NSArray *array = dataDic[@"commentlist"];
        for (NSDictionary *oneDic in array) {
            
            PKCommunityWebModel *model = [[PKCommunityWebModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
        }
        
        _postsinfoDic = dataDic[@"postsinfo"];
        NSString *webStr = _postsinfoDic[@"html"];
        // 修改一下图片的大小
        webStr = [NSString stringWithFormat:@"<head><style>img{width:%fpx !important;}</style></head>%@",kScreenWidth - 15,webStr];
       // 设置内容，这里包装一层div，用来获取内容实际高度（像素），htmlcontent是html格式的字符串
        NSString * htmlcontent = [NSString stringWithFormat:@"<div id=\"webview_content_wrapper\">%@</div>", webStr];
#pragma mark ----  UIWebView的加载方式:2.字符串加载
        [_webView loadHTMLString:htmlcontent baseURL:nil];
        
    
    } error:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
    
    
}

#pragma mark ---- webView的代理方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"webView加载完毕");
    
#pragma mark ----  添加scrollView上的作者的信息
    [self addTopView];
    
    // 修改_mainScrollView的活动范围
#pragma mark ---- 自适应WebView的高度
    float clientheight = [[_webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    NSLog(@"%f",self.height);
    // 设置到WebView上
    _webView.frame = CGRectMake(0, 200, kScreenWidth, clientheight);
    // 获取WebView最佳尺寸（点）
    CGSize frame = [_webView sizeThatFits:_webView.frame.size];
    // 获取内容实际高度（像素）
    NSString * height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('webview_content_wrapper').offsetHeight + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-top'))  + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-bottom'))"];
    float trueHeight = [height_str floatValue];
    
    //内容实际高度（像素）* 点和像素的比
    trueHeight = trueHeight * frame.height / clientheight;
    //再次设置WebView高度（点）
    self.height = trueHeight;
    webView.frame = CGRectMake(0, 200, self.view.frame.size.width, _height);
    
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _height + 220 + 1, kScreenWidth, 1)];
    lineLabel.backgroundColor = [UIColor grayColor];
    [_mainScrollView addSubview:lineLabel];

    
#pragma mark ---- 添加评论表示图
    
    [self addCommListTableView];
    
    [_commitListTableView reloadData];
    
}

// 创建评论的表示图
- (void)addCommListTableView
{
    
    _commitListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _height + 221 + 2, kScreenWidth, 150 * (_listArray.count + 1) ) style:(UITableViewStylePlain)];
    [_mainScrollView addSubview:_commitListTableView];
    _commitListTableView.scrollEnabled = NO;
    _commitListTableView.delegate = self;
    _commitListTableView.dataSource = self;
    _commitListTableView.rowHeight = 150;
    
    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    buttonView.backgroundColor = [UIColor greenColor];
    UIButton *buttoLeft = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    
    buttoLeft.frame = CGRectMake(10, 10, (buttonView.width - 10 * 2 - 20 * 2) / 3, 40);
    buttoLeft.backgroundColor = [UIColor blackColor];
    [buttoLeft setTitle:@"只看楼主" forState:(UIControlStateNormal)];
    buttoLeft.titleLabel.font = [UIFont systemFontOfSize:12];
    [buttonView addSubview:buttoLeft];
    
    UIButton *buttoMiddle = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buttoMiddle.frame = CGRectMake(buttoLeft.right + 20, 10, (kScreenWidth - 20 - 20 * 2) / 3, 40);
    buttoMiddle.backgroundColor = [UIColor blackColor];
    [buttoMiddle setTitle:@"顺序浏览评论" forState:(UIControlStateNormal)];
    buttoMiddle.titleLabel.font = [UIFont systemFontOfSize:12];
    [buttonView addSubview:buttoMiddle];

    
    UIButton *buttoRight = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buttoRight.frame = CGRectMake(buttoMiddle.right + 20 , 10, (kScreenWidth - 20 - 20 * 2) / 3, 40);
    buttoRight.backgroundColor = [UIColor blackColor];
    [buttoRight setTitle:@"热门评论" forState:(UIControlStateNormal)];
    buttoRight.titleLabel.font = [UIFont systemFontOfSize:12];
    [buttonView addSubview:buttoRight];

    _commitListTableView.tableHeaderView = buttonView;
    
    
    // 3D效
    buttoLeft.layer.shadowColor = [[UIColor blackColor]CGColor];
    buttoLeft.layer.shadowOffset = CGSizeMake(4,2);
    buttoLeft.layer.borderColor = [[UIColor blackColor]CGColor];
    buttoLeft.layer.borderWidth = 1;
    // 修改shadow的透明度,不为0就能显示出来
    buttoLeft.layer.shadowOpacity = 1;
    
    buttoMiddle.layer.shadowColor = [[UIColor blackColor]CGColor];
    buttoMiddle.layer.shadowOffset = CGSizeMake(4,2);
    buttoMiddle.layer.borderColor = [[UIColor blackColor]CGColor];
    buttoMiddle.layer.borderWidth = 1;
    // 修改shadow的透明度,不为0就能显示出来
    buttoMiddle.layer.shadowOpacity = 1;

    buttoRight.layer.shadowColor = [[UIColor blackColor]CGColor];
    buttoRight.layer.shadowOffset = CGSizeMake(4,2);
    buttoRight.layer.borderColor = [[UIColor blackColor]CGColor];
    buttoRight.layer.borderWidth = 1;
    // 修改shadow的透明度,不为0就能显示出来
    buttoRight.layer.shadowOpacity = 1;

    
    
    
    // 给3个按钮添加单击事件
    [buttoLeft addTarget:self action:@selector(leftAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [buttoMiddle addTarget:self action:@selector(middleAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [buttoRight addTarget:self action:@selector(rightAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
   
    
#pragma mark ---- 修改_mainScrollView的活动范围
    _mainScrollView.contentSize = CGSizeMake(0, self.height + 221 + _commitListTableView.height);
    
}

#pragma mark ----- tableview的dataSource协议的方法
// 返回cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _listArray.count;
}

// 返回cell的样式:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myCELL";
    PKCommunityCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[PKCommunityCommentTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    // 展示数据
    PKCommunityWebModel *model = _listArray[indexPath.row];
    cell.model = model;

    return cell;
}


#pragma mark --- 添加webview上方的消息框
- (void)addTopView
{
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10*kWidthBase, 10*kHeightBase , kScreenWidth- 20*kWidthBase, 60*kHeightBase)];
    titleLabel.text = _postsinfoDic[@"title"];
    titleLabel.numberOfLines = 0;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.topView addSubview:titleLabel];
    
    UIButton *headView = [UIButton buttonWithType:(UIButtonTypeCustom)];
    headView.frame = CGRectMake((kScreenWidth - 1*kWidthBase)/2, titleLabel.bottom + 15*kHeightBase + 40*kWidthBase, 1*kWidthBase, 1*kWidthBase);
    headView.layer.cornerRadius = 0.5*kWidthBase;
    
    headView.layer.masksToBounds = YES;
    [headView sd_setBackgroundImageWithURL:[NSURL URLWithString:_postsinfoDic[@"userinfo"][@"icon"]] forState:(UIControlStateNormal) placeholderImage:nil];
    [self.topView addSubview:headView];
    
    [UIView animateWithDuration:0.8 animations:^{
        
        headView.frame = CGRectMake((kScreenWidth - 80*kWidthBase)/2, titleLabel.bottom + 15*kHeightBase, 80*kWidthBase, 80*kWidthBase);
        headView.layer.cornerRadius = headView.width / 2;
        
        
    } completion:^(BOOL finished) {
        
        // 在头像上方加一个王冠,表示楼主
        UIImageView *wangguan = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - 30*kWidthBase)/2, titleLabel.bottom, 30*kWidthBase, 30*kWidthBase)];
        // wangguan.backgroundColor = [UIColor redColor];
        wangguan.image = [UIImage imageNamed:@"iconfont-wangguan"];
        [self.topView addSubview:wangguan];
        

        
    }];
    
    
#pragma mark -----  点击头像进入聊天 界面
    [headView addTarget:self action:@selector(chatRoom:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    UILabel *unameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, headView.bottom + 5*kHeightBase , kScreenWidth, self.topView.height - headView.bottom)];
    unameLabel.textAlignment = NSTextAlignmentCenter;
    unameLabel.textColor = [UIColor grayColor];
    unameLabel.text = _postsinfoDic[@"userinfo"][@"uname"];
    unameLabel.font = [UIFont systemFontOfSize:13];
    [self.topView addSubview:unameLabel];
    
    
    
    // 点击评论
    UIButton *commentButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    commentButton.frame = CGRectMake((kScreenWidth - 20*kWidthBase), (headView.height - 20*kWidthBase)/2, 20*kWidthBase, 20*kWidthBase);
    commentButton.alpha = 0;
    commentButton.layer.cornerRadius = 10*kWidthBase;
    commentButton.layer.masksToBounds = YES;
    commentButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [commentButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.topView addSubview:commentButton];
     // 分享
    UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shareButton.frame = commentButton.frame;
    shareButton.alpha = 0;
    shareButton.layer.cornerRadius = 10*kWidthBase;
    shareButton.layer.masksToBounds = YES;
    shareButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [shareButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.topView addSubview:shareButton];

    [commentButton setTitle:@"评论" forState:(UIControlStateNormal)];
    [shareButton setTitle:@"分享" forState:(UIControlStateNormal)];

    
    [UIView animateWithDuration:1 delay:0.3 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
        
        commentButton.alpha = 1;
        shareButton.alpha = 1;
        
        commentButton.frame = CGRectMake(headView.left - 20, headView.top, commentButton.width, commentButton.height);
        shareButton.frame = CGRectMake(headView.right, headView.bottom - shareButton.height, shareButton.width, shareButton.height);
        
    } completion:^(BOOL finished) {
        
    }];
    
    // 给两个按钮添加方法
    [commentButton addTarget:self action:@selector(addComment:) forControlEvents:(UIControlEventTouchUpInside)];
    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

#pragma mark  ----  实现分享功能
- (void)shareAction:(UIButton *)button
{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5689e5f467e58e978f00129a"
                                      shareText:@"分享话题 : "
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToEmail,UMShareToWechatTimeline,nil]
                                       delegate:nil];
}

#pragma mark ---- 点击头像进入聊天室 : 
- (void)chatRoom:(UIButton *)button
{
    
    NSLog(@"进入聊天室");
    
    
}


#pragma mark ---  点击评论展示的效果
- (void)rightAction:(UIButton *)button
{
    // 点击热门评论
    _type = @"hot";
    [self changeCommentType];
    
}

- (void)middleAction:(UIButton *)button
{
    _isAsc = !_isAsc;
    
    
    if (_isAsc) {
        
        // 说明是升序
        _type = @"asc";
        [button setTitle:@"顺序浏览评论" forState:(UIControlStateNormal)];
        
    } else {
        
         _type = @"desc";
        [button setTitle:@"倒叙浏览评论" forState:(UIControlStateNormal)];
        
        
    }
   
    [self changeCommentType];
    
}


-(void)leftAction:(UIButton *)button
{
    _type = @"owner";
    [self changeCommentType];
}

// 重新请求评论数据
- (void)changeCommentType
{
    // 先把评论的数据源清空
    [_listArray removeAllObjects];
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"";
    parDic[@"client"] = @"1";
    parDic[@"contentid"] = _contentid;
    parDic[@"deviceid"] = @"F7EFB566-54F1-4912-ADFC-7E788F6D4D00";
    parDic[@"version"] = @"3.0.6";
    parDic[@"limit"] = @"10";
    parDic[@"sort"] = _type;
    parDic[@"start"] = @"0";
    
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/group/posts_comment" parDic:parDic Method:POST finish:^(NSData *data) {
       
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *listArray = dataDic[@"list"];
        for (NSDictionary *oneDic in listArray) {
            
            
            PKCommunityWebModel *model = [[PKCommunityWebModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
            
            
        }
        
        [self.commitListTableView reloadData];
        
        
    } error:^(NSError *error) {
        
    }];
    
    
}


// 消息框自动消失的实现
- (void)failDismiss:(UIAlertController *)alert
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


// 实现返回按钮
- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
